class Solution {
public:
    int findUnsortedSubarray(vector<int>& nums) {
        int n=nums.size();
        if(n==1)
            return 0;
        int left=n-1,right=0;
        int maxn=nums[0];
        int minn=nums[n-1];
        for(int i=0;i<n;i++)
        {
            if(nums[i]<maxn)
            {
               right=i;
            }
            maxn=max(maxn,nums[i]);
        }
        for(int i=n-1;i>=0;i--)
        {
            if(nums[i]>minn)
            {
                left=i;
            }
            minn=min(minn,nums[i]);
        }
        return max(0,right-left+1);
    }
};
